<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$return['diy']=$this->plugin_setting('diy');
$basic=$this->plugin_setting('basic');
$return['banner']=$basic['image'];
$return['basic']=$basic;
if($_RQ['op']=='main'){
	if(!$_RQ['id']){
		exi('没有找到该项目，请重新选择！','error');
	}
	$project=pdo_get('jy_zhongchou_project',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$project['cailiao']=json_decode($project['cailiao'],true);
	$user = pdo_get('jy_zhongchou_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$project['mid']));
	
	$user['member']=pdo_get('core_members',array('id'=>$project['mid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','groupid','avatar','nickname'));
//	print_r($project['mid'].'fff');exit;
	//添加一次浏览次数
	pdo_update('jy_zhongchou_project',array('look +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$project['createtime']=date('Y-m-d H:i:s',$project['createtime']);
	$project['shouchishenfenz']=json_decode($project['shouchishenfenz'],true);
	$project['hospitalz']=json_decode($project['hospitalz'],true);
	$project['buchong']=json_decode($project['buchong'],true);
	//证明人数据
	$zhengming=pdo_getall('jy_zhongchou_zhengming',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$_RQ['id'],'status'=>1));
	if($zhengming){
		$zhengming_sum=count($zhengming);
		foreach($zhengming as &$zm){
			$zm['member']=pdo_get('core_members',array('id'=>$zm['mid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','groupid','avatar','nickname'));
	
		}
	}
	//捐款数据
	$paylog=pdo_getall('jy_zhongchou_paylog',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$_RQ['id'],'ispay'=>1));
	if($paylog){
		foreach($paylog as &$pl){
			$pl['member']=pdo_get('core_members',array('id'=>$pl['mid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','groupid','avatar','nickname'));
	
		}
	}
	//收藏
	$isshoucang=pdo_getcolumn('jy_zhongchou_shoucang',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$project['id']),'isfollow');
	$this->session('id',$_RQ['id']);
	$project['progress']=$project['getmoney']/$project['goalmoney']*100>100?100:$project['getmoney']/$project['goalmoney']*100;
	//$project['cailiao']=json_decode($project['cailiao'],true);
	$return['user']=$user;
	$return['post']=array();
	$return['project']=$project;
	$return['item']=$project;
	$return['paylog']=$paylog;
	$return['zhengming']=$zhengming;
	$return['zhengming_sum']=$zhengming_sum;
	$return['isshoucang']=$isshoucang;
	$return['sharedata']=array(
		'title'=>$project['title'],
		'desc'=>$project['desc'],
		'link'=>custom_url('jy_zhongchou/p_detail//id='.$_RQ['id'],true),
		'imgUrl'=>ATTACHMENT_URL.$project['image']
	);
	set_html('title',$project['title']);
	exi($return);
}
//收藏
if($_RQ['op']=='follow'){
	$id = $this->session('id');
	if(!$id){
		exi('没有找到该项目，请重新选择！','error');
	}
	$project=pdo_get('jy_zhongchou_project',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$follow=pdo_get('jy_zhongchou_shoucang',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$project['id']));
	if($follow){
		if($follow['isfollow'] !=1){
			pdo_update('jy_zhongchou_shoucang',array('isfollow'=>1),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$project['id']));
			
		}else{
			pdo_update('jy_zhongchou_shoucang',array('isfollow'=>0),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$project['id']));
		}
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'pid'=>$project['id'],
			'mid'=>$_SESSION['uid'],
			'isfollow'=>1,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_zhongchou_shoucang',$insert);
	}
	exi('操作成功');
}

//支持（支付）
if($_RQ['op']=='pay'){
	$id=$_RQ['id'];
	if(!$_RQ['id']){
		$id=$this->session('id');
	}
	if(!$id){
		exi('未正确获取项目id','error');
	}
	
	$post=array();
	$post['fee']=0;
	 $user=pdo_get('jy_zhongchou_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	 $project=pdo_get('jy_zhongchou_project',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if($_RQ['postdata']){
	   $postdata=get_postdata();
	   
	   $fee=$postdata['money'];
	   $msg=$postdata['msg'];
	   if($fee<=0){
		   exi('支持金额需大于0元','error');
	   } 
	   if(!$msg){
		   exi('请输入留言！','error');
	   }

	   //创建支付日志
	   $insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'pid'=>$id,
			'ordersn'=>get_ordersn('jy_zhongchou_paylog','ordersn'),
			'fee'=>$fee,
		    'msg'=>$msg,
			'ispay'=>0,
			'status'=>0,
			'createtime'=>TIMESTAMP
	   );
	   pdo_insert('jy_zhongchou_paylog',$insert);
	   //支付
	   $id=pdo_insertid();
	   $params['fee']=$fee;
	    $params['title']='支持订单';
		$params['id']=$id;
		$params['plugin']='jy_zhongchou';
		$params['back_url']='jy_zhongchou/p_detail/payresult';
		$payid=cfc('pay')->create_pay_order($params);
	}
	$return['post']=$post;
	$return['payid']=$payid;
	exi($return);
}

if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];unset($_RQ['pay_result']);
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	
	$order=pdo_get('jy_zhongchou_paylog',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！订单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_zhongchou_paylog',array('ispay'=>1,'fee'=>$payresult['fee'],'successtime'=>TIMESTAMP),array('id'=>$order['id']));
	$project=pdo_get('jy_zhongchou_project',array('id'=>$order['pid']));
	pdo_update('jy_zhongchou_project',array('getmoney'=>$project['getmoney']+$payresult['fee'],'cash'=>$project['cash']+$payresult['fee']),array('id'=>$order['pid']));

}